package com.czb.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: 陈正彬
 * 创建队列，交换机...
 */
@Configuration
public class RabbitMQConfig {

    //定义队列
    private static final String QUEUE = "queue";

    //fanout
    private static final String QUEUE1 = "queue_fanout1";
    private static final String QUEUE2 = "queue_fanout2";
    private static final String EXCHANGE = "fanoutExchange";

    //direct
    private static final String QUEUE_DIRECT1 = "queue_direct1";
    private static final String QUEUE_DIRECT2 = "queue_direct2";
    private static final String EXCHANGE_DIRECT = "directExchange";

    //路由
    private static final String ROUTING_KET01 = "queue.red";
    private static final String ROUTING_KET02 = "queue.green";

    //创建队列
    @Bean
    public Queue queue() {
        //true 表示持久化(rabbitmq 重启后队列数据还能使用)
        //Erlang自带Mnesia数据库，当rabbitmq重启后会读取该数据库
        //默认是true
        return new Queue(QUEUE, true);
    }

    //创建队列
    @Bean
    public Queue queue1() {

        return new Queue(QUEUE1);
    }

    //创建队列
    @Bean
    public Queue queue2() {

        return new Queue(QUEUE2);
    }

    //创建交换机
    @Bean
    public FanoutExchange exchange() {
        return new FanoutExchange(EXCHANGE);
    }

    //将queue1绑定到exchange
    @Bean
    public Binding binding01() {
        return BindingBuilder.bind(queue1()).to(exchange());
    }

    //将queue2绑定到exchange
    @Bean
    public Binding binding02() {
        return BindingBuilder.bind(queue2()).to(exchange());
    }

    //direct

    //创建队列
    @Bean
    public Queue queue_direct1() {

        return new Queue(QUEUE_DIRECT1);
    }

    //创建队列
    @Bean
    public Queue queue_direct2() {

        return new Queue(QUEUE_DIRECT2);
    }

    //创建交换机
    @Bean
    public DirectExchange exchange_direct() {
        return new DirectExchange(EXCHANGE_DIRECT);
    }

    //将QUEUE_DIRECT1绑定到exchange_direct,同时关联路由ROUTING_KET01
    @Bean
    public Binding binding_direct1() {
        return BindingBuilder.bind(queue_direct1()).to(exchange_direct()).with(ROUTING_KET01);
    }
    //将QUEUE_DIRECT2绑定到exchange_direct,同时关联路由ROUTING_KET02
    @Bean
    public Binding binding_direct2() {
        return BindingBuilder.bind(queue_direct2()).to(exchange_direct()).with(ROUTING_KET02);
    }

}
